//
//  LXCommentViewController.m
//  TuDianEducation
//
//  Created by lixinkeji on 2020/6/30.
//  Copyright © 2020 zhangbenchao. All rights reserved.
//

#import "LXCommentViewController.h"
#import "LXPaySucessViewController.h"
#import "MediaCollectionView.h"
#import "CommentStartView.h"
#import "LXSaveUserInforTool.h"
#import "CommendOrderViews.h"

//#import "UserInforHelper.h"
@interface LXCommentViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic,strong) UIImageView *headerImgView;
@property (nonatomic, strong) CommentStartView *starView;
@property (nonatomic, strong) UITextView *cancellReTextView;
@property (nonatomic, strong) UILabel *placeHoder;
@property (nonatomic,strong) MediaCollectionView *mediaView;

@property (nonatomic, strong) UIButton *ensureBtn;

@property (nonatomic,strong) NSArray *messageArray;
@property (nonatomic,strong) NSMutableArray *uiMessageArray;

@property (nonatomic,strong) NSArray *evaluateList;
@property (nonatomic,strong) UIScrollView *contenScrollView;
@property (nonatomic,assign) CGFloat okbottom;
@end

@implementation LXCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"去评价";
    self.view.backgroundColor = kWhiteColor;
    [self.view addSubview:self.contenScrollView];
    
    self.messageArray = self.dataArray;;
    
   
}
-(UIScrollView *)contenScrollView
{
    if (!_contenScrollView) {
        _contenScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, TSScreenWidth, TSScreenHeight - Height_NavBar)];
        _contenScrollView.delegate = self;
        _contenScrollView.backgroundColor = kWhiteColor;
    }
    return _contenScrollView;
}


-(UIButton *)ensureBtn
{
    if (!_ensureBtn) {
        _ensureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _ensureBtn.frame = CGRectMake(TSScaleW(11),  _okbottom, TSScreenWidth - TSScaleW(22),TSScaleW(40));
        [_ensureBtn btn:_ensureBtn font:TSScaleW(14) textColor:kWhiteColor text:@"提交评论" image:nil sel:@selector(ensureBtnAction:) taget:self];
        _ensureBtn.backgroundColor = kGreenColor;
        _ensureBtn.cornerRadius = _ensureBtn.height/2.f;
    }
    return _ensureBtn;
}

-(void)ensureBtnAction:(UIButton *)sender
{

    [self orderevaluateViews];
}

-(void)dissmiss
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)setMessageArray:(NSArray *)messageArray{
    _messageArray = messageArray;
    for (UIView *v  in self.uiMessageArray) {
        [v removeFromSuperview];
    }
    [self.uiMessageArray removeAllObjects];
    CGFloat top = TSScaleW(20);
    _okbottom = 0;
    for (int i = 0; i <self.messageArray.count; i ++) {
        NSDictionary *dataDic = self.messageArray[i];
        CommendOrderViews *messageView = [[CommendOrderViews alloc]init];
        messageView.top = top;
        top += messageView.height;
        _okbottom = messageView.bottom + TSScaleW(60);
        messageView.dataDic = dataDic;
        [self.contenScrollView addSubview:messageView];
        [self.uiMessageArray addObject:messageView];
    }
   
   // self.totalAmountLabel.text = [NSString stringWithFormat:@"@"共1件商品""]
    [self.contenScrollView addSubview:self.ensureBtn];
    self.contenScrollView.contentSize = CGSizeMake(0, self.ensureBtn.bottom + TSScaleW(200));
    
}
-(NSMutableArray *)uiMessageArray{
    if (!_uiMessageArray) {
        _uiMessageArray = [NSMutableArray array];
    }
    return _uiMessageArray;
}
-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    self.messageArray = _dataArray;
}

-(NSArray *)evaluateList{
    NSMutableArray *array = [NSMutableArray array];
    for (CommendOrderViews *messageView in self.uiMessageArray) {
        [array addObject:messageView.requstDataDic];
    }
    return array;
}
#pragma mark ----订单评价
-(void)orderevaluateViews
{
    for (NSDictionary *dic in [self evaluateList]) {
        if (dic.allKeys.count != 4) {
            [MBProgressHUD showError:@"请输入完整信息"];
            return;
        }
    }
    NSDictionary * pamas = @{@"ordernum":_ordernum,@"evaluateList":[self evaluateList]};
       [NetWorkTools postConJSONWithUrl:@"orderevaluate" parameters:pamas success:^(id responseObject) {
           NSString *result = responseObject[@"result"];
           NSString *resultNote = responseObject[@"resultNote"];
           NSArray *array = responseObject[@"dataList"];
          
           if (result.integerValue == 0) {
               
               [self.navigationController popViewControllerAnimated:YES];
           }else{
               
           }
           [MBProgressHUD showError:resultNote];
       } fail:^(NSError *error) {
           
       }];
}

@end
